package com.com.Druidconfig;


import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import javax.servlet.annotation.WebFilter;
import java.util.HashMap;

@Configuration
public class DruidConfig {


//注册DruidDataSource数据源
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
   public DruidDataSource druidDataSource(){
        return new DruidDataSource();
    }

    //后台监控：web.xml  ServletRegistrationBean
    //因为springboot 内置了servlet容器，所以没有web.xml  。就需要用ServletRegistrationBean去替代
    @Bean
    public ServletRegistrationBean registrationBean(){
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");

        //后台需要有人登入，配置账号密码
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("loginUsername","root");//固定写法
        map.put("loginPassword","root");

        map.put("allow","");//允许所有人访问
        bean.setLoadOnStartup(1);

        bean.setInitParameters(map);
        return bean;
    }
    //配置过滤器
    public FilterRegistrationBean filterRegistrationBean(){

        FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new WebStatFilter());
        HashMap<String, String> map = new HashMap<>();
        bean.setInitParameters(map);
        map.put("exclusions","*.css,*.js,*.html");
        return bean;
    }
}
